/**
 * 帖子详情显示相关函数
 * 
 * @author		布衣才子
 * @date		2012-10-15
 * @copyright	2012-2015 YeeYi.com 
 * @email		work.jerryliu@gmail.com
 * @QQ			394969553
 */



var forumHost = "http://www.yeeyi.com/bbs/forum.php?jsoncallback=?&mod=mobile";
var b64 = new Base64();
var confirmNum = 0;

/**
 * 网络请求中转函数
 * 
 * @param {Object} url  请求的网络地址
 * @param {Object} callback  回调函数
 */
function xmlHttp(url,callback){
	if(url == ''){
		uexWindow.alert('参数错误','请求地址不能为空！','返回');
	}else{
		var platform = getLocalStorge('platform');
		url = url + "&pf="+platform;
		$.getJSON(url,callback);
	}
}

/**
 * 添加到收藏夹
 */
function addToFavorite(){
	var tid = getLocalStorge('tid');
	var uid = getLocalStorge('uid');
	var password = getLocalStorge('pwd');
	if(!tid){
		uexWindow.alert('提示','缺少参数,请重新操作!','返回');
		return false;
	}
	
	if(!tid){
		uexWindow.alert('提示','您还未登陆，请先登录！','返回');
		return false;
	}
	
	uexWindow.toast("1","5","正在添加到收藏…","0");
	
	var requestURL = forumHost + '&ac=favorite&tid='+tid+'&uid='+uid+'&pwd='+password;
	xmlHttp(requestURL,showReturn);
}

/**
 * 收藏的回调函数
 * @param {Object} data
 */
function showReturn(data){
	var returnCode = parseInt(data.info);
	if(returnCode > 1000){
		uexWindow.closeToast();
		uexWindow.toast("0","5","收藏成功…","2000");
	}else if(returnCode == 401){
		uexWindow.closeToast();
		uexWindow.toast("0","5","收藏失败，用户验证失败…","2000");
	}else if(returnCode == 404){
		uexWindow.closeToast();
		uexWindow.toast("0","5","收藏失败，未找到您要收藏的帖子…","2000");
	}else{
		uexWindow.closeToast();
		uexWindow.toast("0","5","收藏失败，未知错误…","2000");
	}
}

function showManage(){
	var uid = getLocalStorge('uid');
	var authorid = getLocalStorge('authorid');
	var groupid = getLocalStorge('groupid');
	var fid = getLocalStorge('fid');
	
	if(authorid == uid){
		var upFids = ['3','316','627','626','625','624','623','622','304','305','635','634','633','325','632','631','630','309','598','604','89','641','642','643'];
		if(in_array(fid,upFids,false)){
			var menulist=new Array(1);
			menulist[0] = "提升帖子";
			uexWindow.actionSheet("帖子操作","返 回",menulist);
		}else{
			uexWindow.toast("0","5","暂时没有啦！","2000");
		}
		
	}else{
		if(in_array(groupid,['1','2','3','57','62','42'])){
			var menulist=new Array(2);
			menulist[0] = "删除帖子";
			menulist[1] = "用户禁言";
			uexWindow.actionSheet("帖子操作","返 回",menulist);
		}else{
			uexWindow.toast("0","5","暂时没有啦！","2000");
		}
	}
}

function actionSheetSuccess(opId,dataType,data){
	
		var groupid = getLocalStorge('groupid');
		var uid = getLocalStorge('uid');
		var authorid = getLocalStorge('authorid');
		var tid = getLocalStorge('tid');
		var password = getLocalStorge('pwd');
		
		
		if(authorid == uid){
			if (data == 0) {
				if(!tid){
					uexWindow.toast("0","5","失败！帖子ID丢失！","2000");
					return false;
				}
				uexWindow.toast("1", "5", "正在进行操作……！", "0");
				var requestUrl = forumHost + '&ac=upthread&tid=' + tid + '&pwd=' + password + '&uid=' + uid + '&groupid=' + groupid;
				xmlHttp(requestUrl, showUpResult);
			}
		}else{
			if(in_array(groupid,['1','2','3','57','62','42'])){
				var confirmButtom = new Array();
				confirmButtom[0] = '确定';
				confirmButtom[1] = '取消';
				if(data == 0){
					if(!tid){
						uexWindow.toast("0","5","失败！帖子ID丢失！","2000");
						return false;
					}
					
					confirmNum = 1;
					uexWindow.confirm('提示：','您确定要删除该帖子？',confirmButtom);
				}else if(data == 1){
					confirmNum = 2;
					uexWindow.confirm('提示：','您确定要对该用户禁言？',confirmButtom);
				}
			}else{
				uexWindow.toast("0","5","抱歉，你没有管理权限！","2000");
			}
		}
}

function ConfirmSuccess(opId,dataType,data){
	var tid = getLocalStorge('tid');
	var uid = getLocalStorge('uid');
	var password = getLocalStorge('pwd');
	if(confirmNum == 1){
		if(data == 0){
			uexWindow.toast("1", "5", "正在进行操作……", "0");
			var delURL = forumHost + '&ac=delete&tid=' + tid + '&pwd=' + password + '&uid=' + uid;
			xmlHttp(delURL, deleteThread);
		}
	}else if(confirmNum == 2){
		if (data == 0) {
			uexWindow.toast("1", "5", "正在进行操作……", "0");
			var authorid = getLocalStorge('authorid');
			var banURL = forumHost + '&ac=ban&authorid=' + authorid + '&pwd=' + password + '&uid=' + uid;
			xmlHttp(banURL, banUser);
		}
	}else if(confirmNum == 3){
		if (data == 0) {
			localStorage.setItem('returnType','viewthread');
			uexWindow.open('login','0','login.html','1','100%','100%','0');
		}
	}
}

function addNewThread(){
	var uid = getLocalStorge("uid");
	if(!uid){
		confirmNum = 3;
		var confirmButtom = new Array();
		confirmButtom[0] = '登录';
		confirmButtom[1] = '返回';
		uexWindow.confirm('提示：','请登录后再回复！',confirmButtom);
		return false;
	}else{
		uexWindow.open('newthread','0','newthread.html','2','100%','100%','0');
	}
}

function closeWindow(){
	uexWindow.close(-1);
}

/**
 * 版主或管理员删除帖子
 * @param {string} data 
 */
function deleteThread(data){
	uexWindow.closeToast();
	var returnCode = data.info;
	if(returnCode == '200'){
		uexWindow.toast("0","5","帖子删除成功,请返回刷新…","2000");
		//uexWindow.close(-1);
	}else if(returnCode == '401'){
		uexWindow.toast("0","5","操作失败，用户名或密码错误…","2000");
	}else if(returnCode == '402'){
		uexWindow.toast("0","5","操作失败，无权操作管理组帖子…","2000");
	}else if(returnCode == '403'){
		uexWindow.toast("0","5","操作失败，您没有该权限…","2000");
	}else if(returnCode == '404'){
		uexWindow.toast("0","5","操作失败，帖子ID丢失…","2000");
	}else{
		uexWindow.toast("0","5","操作失败，未知错误…","2000");
	}
}

/**
 * 版主或管理员禁言用户
 * @param {string} data 
 */
function banUser(data){
	uexWindow.closeToast();
	var returnCode = data.info;
	if(returnCode == '200'){
		uexWindow.toast("0","5","禁言用户成功…","2000");
	}else if(returnCode == '401'){
		uexWindow.toast("0","5","操作失败，用户名或密码错误…","2000");
	}else if(returnCode == '402'){
		uexWindow.toast("0","5","操作失败，无权操作管理组用户…","2000");
	}else if(returnCode == '403'){
		uexWindow.toast("0","5","操作失败，您没有该权限…","2000");
	}else{
		uexWindow.toast("0","5","操作失败，未知错误…","2000");
	}
}

/**
 * 处理帖子提升返回的结果
 * @param {string} data
 */
function showUpResult(data){
	uexWindow.closeToast();
	var returnCode = data.info;
	if(returnCode == '200'){
		uexWindow.toast("0","5","帖子提升成功…","2000");
	}else if(returnCode == '401'){
		uexWindow.toast("0","5","提升失败，用户名或密码错误…","2000");
	}else if(returnCode == '404'){
		uexWindow.toast("0","5","提升失败，帖子ID缺失…","2000");
	}else if(returnCode == '405'){
		uexWindow.toast("0","5","提升失败，只有楼主才可以提升…","2000");
	}else if(returnCode == '406'){
		uexWindow.toast("0","5","提升失败，该板块没有提升功能…","2000");
	}else if(returnCode == '407'){
		uexWindow.toast("0","5","提升失败，每次提升至少间隔10分钟…","2000");
	}else if(returnCode == '408'){
		uexWindow.toast("0","5","提升失败，您的YB余额不足…","2000");
	}else{
		uexWindow.toast("0","5","提升失败，未知错误…","2000");	
	}
}

function fastReply(){
		var uid = getLocalStorge('uid');
		if(!uid){
			confirmNum = 3;
			var confirmButtom = new Array();
			confirmButtom[0] = '登录';
			confirmButtom[1] = '返回';
			uexWindow.confirm('提示：','请登录后再回复！',confirmButtom);
			return false;
		}
		
		uexWindow.toast("1","5","回复内容正在发布…","0");
		var allAid = '';
		
		var message = $('#fastReply').val();
		var fid = getLocalStorge('fid');
		var username = getLocalStorge('username');
		var password = getLocalStorge('pwd');
		var tid = getLocalStorge('tid');
		
		
		if(message.length == 0){
			uexWindow.closeToast();
			uexWindow.toast("0","8","内容不能为空…","2000");
			return false;
		}
		
		if(!fid){
			uexWindow.closeToast();
			uexWindow.toast("0","5","提交失败，没有找到对应板块…","2000");
			return false;
		}
		
		if(!tid){
			uexWindow.closeToast();
			uexWindow.toast("0","5","提交失败，帖子ID信息丢失…","2000");
			return false;
		}
		
		if(!uid){
			uexWindow.closeToast();
			uexWindow.toast("0","5","提交失败，用户UID缺失…","2000");
			return false;
		}
		
		localStorage.setItem('fastReply',message);
		_message = encodeURIComponent(b64.encode(message));
		_region = '';
		_imgaid = '';
		_username = encodeURIComponent(b64.encode(username));
		_password = encodeURIComponent(password);
		
		var RequestUrl = forumHost + '&ac=reply&tid='+tid+'&message='+_message+'&fid='+fid+'&imgaid='+_imgaid+'&uid='+uid+'&username='+_username+'&password='+_password+'&region='+_region;
		
		xmlHttp(RequestUrl,replyCallBack);
}

function replyCallBack(data){
	uexWindow.closeToast();
	var returnInfo = data.info;
	if(parseInt(returnInfo) > 10000){
		uexWindow.toast("0","5","回复成功…","2000");
		uexWindow.evaluatePopoverScript("threadWindow","content","fastAppendReply("+returnInfo+")");
		$('#fastReply').val('');
		$('.fastbutton').hide();
	}else if(returnInfo == '403'){
		uexWindow.toast("0","5","发布失败，用户名或密码错误…","2000");
	}else if(returnInfo == '405'){
		uexWindow.toast("0","5","发布失败，您的用户组无权发帖…","2000");
	}else if(returnInfo == '406'){
		uexWindow.toast("0","5","发布失败，缺少参数…","2000");
	}else{
		uexWindow.toast("0","5","发布失败，请稍后重试…","2000");
	}
}

function showMenu(){
	var uid = getLocalStorge('uid');
	if(!uid){
		confirmNum = 3;
		var confirmButtom = new Array();
		confirmButtom[0] = '登录';
		confirmButtom[1] = '返回';
		uexWindow.confirm('提示：','您还未登陆，是否登录？',confirmButtom);
		return false;
	}else{
		uexWindow.evaluatePopoverScript('','content','showMenu()');
	}
}

function getLocalStorge(key){
	var item = localStorage.getItem(key);
	if(item=='' || item==null || typeof(item)=="undefined"){
		return false;
	}else{
		return item;
	}
}

function in_array (needle, haystack, argStrict) {
    var key = '',
		strict = !! argStrict;

    if (strict) {
        for (key in haystack) {
            if (haystack[key] === needle) {
                return true;
            }
        }
    } else {
        for (key in haystack) {
            if (haystack[key] == needle) {
                return true;
            }
        }
    }

    return false;
}